﻿using DotNetMVC.DTO;
using DotNetMVC.IService;
using DotNetMVC.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace DotNetMVC.Service
{
    public partial class SiteContentService : BaseService<SiteContent>, ISiteContentService
    {
        private IQueryable<SiteContent> GetJoinAll()
        {
            return GetAll()
                .Include(u => u.SiteNav)
                .Include(u => u.SiteNav.FunctionalModule)
                .AsNoTracking();
        }

        public SiteContentDTO GetDTOById(long id)
        {
            var siteContents = GetJoinAll().Where(e => e.Id == id);
            int count = siteContents.Count();
            if (count <= 0)
            {
                return null;
            }
            else if (count == 1)
            {
                return ToDTO(siteContents.Single());
            }
            else
            {
                throw new ApplicationException("获取到多个 Id 为" + id + "的内容信息");
            }
        }


        public SiteContentDTO[] GetDTOPagedData(long navId, out long total, int pageIndex, int pageSize)
        {
            IQueryable<SiteContent> entities = GetJoinAll().Where(e => e.SiteNavId == navId);
            total = entities.Count();
            return entities.OrderByDescending(e => e.Sort).ThenByDescending(e => e.SubmitDate).ThenByDescending(e => e.Id)
                .Skip(pageSize * (pageIndex - 1))
                .Take(pageSize)
                .ToList()
                .Select(e => ToDTO(e))
                .ToArray();
        }

        public SiteContentDTO[] GetDTOByNavId(long navId)
        {
            return GetJoinAll().Where(e => e.SiteNavId == navId).OrderByDescending(e => e.Sort).ThenByDescending(e => e.SubmitDate).ThenByDescending(e => e.Id)
                .ToList()
                .Select(e => ToDTO(e))
                .ToArray();
        }

        public SiteContentDTO[] GetDTOByNavId(long navId, int count)
        {
            return GetJoinAll().Where(e => e.SiteNavId == navId).OrderByDescending(e => e.Sort).ThenByDescending(e => e.SubmitDate).ThenByDescending(e => e.Id)
                .Take(count)
                .ToList()
                .Select(e => ToDTO(e))
                .ToArray();
        }

        public SiteContentDTO[] GetDTOByFunCode(string funCode)
        {
            return GetJoinAll().Where(e => e.SiteNav.FunctionalModule.FunCode == funCode).ToList().Select(e => ToDTO(e)).ToArray();
        }

        private SiteContentDTO ToDTO(SiteContent entity)
        {
            SiteContentDTO dto = new SiteContentDTO();
            dto.CreateDateTime = entity.CreateDateTime;
            dto.Id = entity.Id;
            dto.SiteNavId = entity.SiteNavId;
            dto.NavName = entity.SiteNav.NavName;
            dto.Keywords = entity.Keywords;
            dto.Description = entity.Description;
            dto.FileName = entity.FileName;
            dto.Title = entity.Title;
            dto.SubmitDate = entity.SubmitDate;
            dto.Cont = entity.Cont;
            dto.LinksUrl = entity.LinksUrl;
            dto.LinksTarget = entity.LinksTarget;
            dto.LinksTargetName = entity.LinksTarget== "_self"?"原窗口打开": "弹出新窗口";
            dto.DownloadFile = entity.DownloadFile;
            dto.ImgLG = entity.ImgLG;
            dto.ImgMD = entity.ImgMD;
            dto.ImgSM = entity.ImgSM;
            dto.ImgXS = entity.ImgXS;
            dto.Sort = entity.Sort;
            dto.IsEnabled = entity.IsEnabled;
            dto.IsShow = entity.IsShow;
            dto.Remark = entity.Remark;
            //生成静态页模板
            dto.ViewHtmlTemplate = entity.SiteNav.FunctionalModule.ViewHtmlTemplate;
            if (!string.IsNullOrEmpty(entity.SiteNav.ViewHtmlTemplate))
            {
                dto.ViewHtmlTemplate = entity.SiteNav.ViewHtmlTemplate;
            }
            dto.BannerImg = "/upload/images/BannerInner.jpg";
            if (!string.IsNullOrEmpty(entity.SiteNav.NavImg)&& !entity.SiteNav.NavImg.Contains("noPic.jpg"))
            {
                dto.BannerImg = entity.SiteNav.NavImg;
            }
            return dto;
        }


        #region 介绍页
        public SiteContent GetInfoByNavId(long navId)
        {
            var infos = GetAll().Where(e => e.SiteNavId == navId && e.SiteNav.FunctionalModule.FunCode == "Info");
            int count = infos.Count();
            if (count <= 0)
            {
                return null;
            }
            else if (count == 1)
            {
                return infos.Single();
            }
            else
            {
                throw new ApplicationException("获取到多个 SiteNavId 为" + navId + "的介绍页");
            }
        }

        public SiteContentDTO GetInfoDTOByNavId(long navId)
        {
            var info = GetInfoByNavId(navId);
            if (info == null)
            {
                return null;
            }
            else
            {
                return ToDTO(info);
            }
        }

        public void UpdateInfo(long id, string cont)
        {
            SiteContent entity = GetById(id);
            if (entity == null)
            {
                throw new ApplicationException("获取到不到 Id 为" + id + "的介绍页信息");
            }
            entity.SubmitDate = DateTime.Now;
            entity.Cont = cont;
            Update(entity);
        }
        #endregion


        #region 新闻资讯
        public SiteContent AddNews(long siteNavId, string title, DateTime submitDate, string cont, string imgLG, int sort)
        {
            SiteContent entity = new SiteContent();
            entity.SiteNavId = siteNavId;
            entity.Title = title;
            entity.SubmitDate = submitDate;
            entity.Cont = cont;
            entity.ImgLG = imgLG;
            entity.Sort = sort;
            entity.IsEnabled = true;
            entity.IsShow = true;
            entity= Add(entity);

            //更新链接地址
            entity.LinksUrl = "/News/"+ siteNavId + "/"+ entity.Id+".html";
            Update(entity);

            return entity;
        }
        public void UpdateNews(long id, string title, DateTime submitDate, string cont, string imgLG, int sort)
        {
            SiteContent entity = GetById(id);
            if (entity == null)
            {
                throw new ApplicationException("获取到不到 Id 为" + id + "的新闻资讯信息");
            }
            entity.Title = title;
            entity.SubmitDate = submitDate;
            entity.Cont = cont;
            entity.ImgLG = imgLG;
            entity.Sort = sort;
            //更新链接地址
            entity.LinksUrl = "/News/" + entity.SiteNavId + "/" + entity.Id + ".html";
            Update(entity);
        }
        #endregion

        #region 资料下载
        public SiteContent AddDownload(long siteNavId, string title, DateTime submitDate, string downloadFile, string imgLG, int sort)
        {
            SiteContent entity = new SiteContent();
            entity.SiteNavId = siteNavId;
            entity.Title = title;
            entity.SubmitDate = submitDate;
            entity.DownloadFile = downloadFile;
            entity.ImgLG = imgLG;
            entity.Sort = sort;
            entity.IsEnabled = true;
            entity.IsShow = true;
            return Add(entity);
        }
        public void UpdateDownload(long id, string title, DateTime submitDate, string downloadFile, string imgLG, int sort)
        {
            SiteContent entity = GetById(id);
            if (entity == null)
            {
                throw new ApplicationException("获取到不到 Id 为" + id + "的资料下载信息");
            }
            entity.Title = title;
            entity.SubmitDate = submitDate;
            entity.DownloadFile = downloadFile;
            entity.ImgLG = imgLG;
            entity.Sort = sort;
            Update(entity);
        }
        #endregion

        #region 友情链接
        public SiteContent AddLink(long siteNavId, string title,  string linksUrl, string listLinksTarget, string imgLG, int sort)
        {
            SiteContent entity = new SiteContent();
            entity.SiteNavId = siteNavId;
            entity.Title = title;
            entity.SubmitDate =DateTime.Now;
            entity.LinksUrl = linksUrl;
            entity.LinksTarget = listLinksTarget;
            entity.ImgLG = imgLG;
            entity.Sort = sort;
            entity.IsEnabled = true;
            entity.IsShow = true;
            return Add(entity);
        }
        public void UpdateLink(long id, string title, string linksUrl, string listLinksTarget, string imgLG, int sort)
        {
            SiteContent entity = GetById(id);
            if (entity == null)
            {
                throw new ApplicationException("获取到不到 Id 为" + id + "的友情链接信息");
            }
            entity.Title = title;
            entity.SubmitDate = DateTime.Now;
            entity.LinksUrl = linksUrl;
            entity.LinksTarget = listLinksTarget;
            entity.ImgLG = imgLG;
            entity.Sort = sort;
            Update(entity);
        }
        #endregion

        #region 图片展示
        public SiteContent AddImage(long siteNavId, string title, string linksUrl, string listLinksTarget, string imgLG, int sort)
        {
            SiteContent entity = new SiteContent();
            entity.SiteNavId = siteNavId;
            entity.Title = title;
            entity.SubmitDate = DateTime.Now;
            entity.LinksUrl = linksUrl;
            entity.LinksTarget = listLinksTarget;
            entity.ImgLG = imgLG;
            entity.Sort = sort;
            entity.IsEnabled = true;
            entity.IsShow = true;
            return Add(entity);
        }
        public void UpdateImage(long id, string title, string linksUrl, string listLinksTarget, string imgLG, int sort)
        {
            SiteContent entity = GetById(id);
            if (entity == null)
            {
                throw new ApplicationException("获取到不到 Id 为" + id + "的图片展示信息");
            }
            entity.Title = title;
            entity.SubmitDate = DateTime.Now;
            entity.LinksUrl = linksUrl;
            entity.LinksTarget = listLinksTarget;
            entity.ImgLG = imgLG;
            entity.Sort = sort;
            Update(entity);
        }
        #endregion

        public void Add(long siteNavId, string fileName, string title, DateTime submitDate, string cont, string linksUrl, string downloadFile, string imgLG, int sort)
        {
            SiteContent entity = new SiteContent();
            entity.SiteNavId = siteNavId;
            entity.FileName = fileName;
            entity.Title = title;
            entity.SubmitDate = submitDate;
            entity.Cont = cont;
            entity.LinksUrl = linksUrl;
            entity.DownloadFile = downloadFile;
            entity.ImgLG = imgLG;
            entity.Sort = sort;
            entity.IsEnabled = true;
            entity.IsShow = true;
            Add(entity);

        }
        public void Update(long id, long siteNavId, string fileName, string title, DateTime submitDate, string cont, string linksUrl, string downloadFile, string imgLG, int sort)
        {
            SiteContent entity = GetById(id);
            if (entity == null)
            {
                throw new ApplicationException("获取到不到 Id 为" + id + "的内容信息");
            }
            entity.SiteNavId = siteNavId;
            entity.FileName = fileName;
            entity.Title = title;
            entity.SubmitDate = submitDate;
            entity.Cont = cont;
            entity.LinksUrl = linksUrl;
            entity.DownloadFile = downloadFile;
            entity.ImgLG = imgLG;
            entity.Sort = sort;
            Update(entity);
        }
    }
}
